/*
* @Time    : 2018/1/11 10:50
* @Author  : xincheng.zhao
* @Software: PyCharm
*/

$(function () {
    var echaert = echarts.init(document.getElementById('main'))
    var option;
    option = {
        title: {
            text: '油箱油液高度和温度',
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#283b56'
                }
            }
        },
        legend: {
            data: ['温度', '液位']
        },
        toolbox: {
            show: true,
            feature: {
                dataView: {readOnly: false},
                restore: {},
                saveAsImage: {}
            }
        },
        dataZoom: {
            show: false,
            start: 0,
            end: 100
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: true,
                data: (function () {
                    var now = new Date();
                    var res = [];
                    var len = 10;
                    while (len--) {
                        res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
                        now = new Date(now - 2000);
                    }
                    return res;
                })()
            },
            {
                type: 'category',
                boundaryGap: true,
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        res.push(++len);
                    }
                    return res;
                })()
            }
        ],
        yAxis: [
            {
                type: 'value',
                scale: true,
                name: '温度℃',
                max: 70,
                min: -20,
                boundaryGap: [0.2, 0.2]
            },
            {
                type: 'value',
                scale: true,
                name: '液位L',
                max: 1200,
                min: 0,
                boundaryGap: [0.2, 0.2]
            }
        ],
        series: [
            {
                name: '液位',
                type: 'bar',
                xAxisIndex: 1,
                yAxisIndex: 1,
                data: (function () {
                    var res = [];
                    var len = 10;
                    while (len--) {
                        var value = Math.round(Math.random() * 1000)
                        res.push(value);
                    }
                    return res;
                })()
            },
            {
                name: '温度',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * -20 + Math.random() *50;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }
        ]
    };

    count = 11;
    setInterval(function () {
        axisData = (new Date()).toLocaleTimeString().replace(/^\D*/, '');
        var data0 = option.series[0].data;
        var data1 = option.series[1].data;
        data0.shift();
        data0.push(Math.round(Math.random() * 1000));
        data1.shift();
        data1.push((Math.random() * -20 + Math.random() *50).toFixed(1) - 0);
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        option.xAxis[1].data.shift();
        option.xAxis[1].data.push(count++);
        echaert.setOption(option);
        if (data0[9] < 200) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，油箱油液液位异常，请检查油箱')
            fault_info('油箱油液液位异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data1[9] < -10 || data1[9] > 50) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，油箱油液温度异常')
            fault_info('油箱油液温度异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
    }, 4000);

    // 使用配置项和数据显示图表。
    echaert.setOption(option);

})

$(function () {
    var echaert = echarts.init(document.getElementById('ybpj'))
    var option;
    option = {

        toolbox: {
            show: true,
            feature: {
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        series: [
            {
                name: '进油压',
                type: 'gauge',
                detail: {formatter: '{value}%'},
                data: [{value: 50, name: '进油压百分比'}]
            },

        ]
    };

    setInterval(function () {
        option.series[0].data[0].value = (Math.random() * 80).toFixed(2) - 0;
        if (option.series[0].data[0].value > 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，进油压有异常，请及时处理')
        }

        echaert.setOption(option, true);
    }, 2000);
})
$(function () {
    var echaert = echarts.init(document.getElementById('ybpctr'))
    var option;
    option = {

        toolbox: {
            show: true,
            feature: {
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        width: 123,
        series: [
            {
                name: '控制油压',
                type: 'gauge',
                detail: {formatter: '{value}%'},
                data: [{value: 50, name: '控制油压百分比'}]
            },

        ]
    };

    setInterval(function () {
        option.series[0].data[0].value = (Math.random() * 80).toFixed(2) - 0;
        if (option.series[0].data[0].value > 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，控制油压异常，请及时处理')
            fault_info('控制油压异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        echaert.setOption(option, true);
    }, 2000);
})
$(function () {
    var echaert = echarts.init(document.getElementById('ybph'))
    var option;
    option = {

        toolbox: {
            show: true,
            feature: {
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        series: [
            {
                name: '回油压',
                type: 'gauge',
                detail: {formatter: '{value}%'},
                data: [{value: 50, name: '回油压百分比'}]
            },

        ]
    };

    setInterval(function () {
        option.series[0].data[0].value = (Math.random() * 80).toFixed(2) - 0;
        if (option.series[0].data[0].value > 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，回油压异常，请及时处理')
            fault_info('回油压异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        echaert.setOption(option, true);
    }, 2000);
})

$(function () {
    var echaert = echarts.init(document.getElementById('direction'))
    var option;
    option = {
        title: {
            text: '转向压力',
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#283b56'
                }
            }
        },
        legend: {
            data: ['转向液压缸入口', '转向液压缸出口', '转向连杆液压缸入口', '转向连杆液压缸出口']
        },
        toolbox: {
            show: true,
            feature: {
                dataView: {readOnly: false},
                restore: {},
                saveAsImage: {}
            }
        },
        dataZoom: {
            show: false,
            start: 0,
            end: 100
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: true,
                data: (function () {
                    var now = new Date();
                    var res = [];
                    var len = 10;
                    while (len--) {
                        res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
                        now = new Date(now - 2000);
                    }
                    return res;
                })()
            },
        ],
        yAxis: [
            {
                type: 'value',
                scale: true,
                name: '压力百分比',
                max: 100,
                min: 0,
            },
        ],
        series: [
            {
                name: '转向液压缸入口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '转向液压缸出口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '转向连杆液压缸入口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '转向连杆液压缸出口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            },
        ]
    };
// 模拟传感器数据，实时更新
    count = 11;
    setInterval(function () {
        axisData = (new Date()).toLocaleTimeString().replace(/^\D*/, '');
        var data0 = option.series[0].data;
        data0.shift();
        data0.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data1 = option.series[1].data;
        data1.shift();
        data1.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data2 = option.series[2].data;
        data2.shift();
        data2.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data3 = option.series[3].data;
        data3.shift();
        data3.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);

        if (data0[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[0] + ',请检查')
            fault_info(option.legend.data[0]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data1[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[1] + ',请检查')
            fault_info(option.legend.data[1]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data2[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[2] + ',请检查')
            fault_info(option.legend.data[2]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data3[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[3] + ',请检查')
            fault_info(option.legend.data[3]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        echaert.setOption(option);
    }, 4000);

    // 使用配置项和数据显示图表。
    echaert.setOption(option);
})


$(function () {
    var echaert = echarts.init(document.getElementById('expansion'))
    var option;
    option = {
        title: {
            text: '扩桥液压',
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#283b56'
                }
            }
        },
        legend: {
            data: ['左扩桥液压缸入口', '左扩桥液压缸出口', '右扩桥液压缸入口', '右扩桥液压缸出口']
        },
        toolbox: {
            show: true,
            feature: {
                dataView: {readOnly: false},
                restore: {},
                saveAsImage: {}
            }
        },
        dataZoom: {
            show: false,
            start: 0,
            end: 100
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: true,
                data: (function () {
                    var now = new Date();
                    var res = [];
                    var len = 10;
                    while (len--) {
                        res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
                        now = new Date(now - 2000);
                    }
                    return res;
                })()
            },
        ],
        yAxis: [
            {
                type: 'value',
                scale: true,
                name: '压力百分比',
                max: 100,
                min: 0,
            },
        ],
        series: [
            {
                name: '左扩桥液压缸入口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '左扩桥液压缸出口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '右扩桥液压缸入口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            }, {
                name: '右扩桥液压缸出口',
                type: 'line',
                data: (function () {
                    var res = [];
                    var len = 0;
                    while (len < 10) {
                        var value = Math.random() * 80;
                        res.push(value.toFixed(1) - 0);
                        len++;
                    }
                    return res;
                })()
            },
        ]
    };

    count = 11;
    setInterval(function () {
        axisData = (new Date()).toLocaleTimeString().replace(/^\D*/, '');
        var data0 = option.series[0].data;
        data0.shift();
        data0.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data1 = option.series[1].data;
        data1.shift();
        data1.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data2 = option.series[2].data;
        data2.shift();
        data2.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);
        var data3 = option.series[3].data;
        data3.shift();
        data3.push(Math.round(Math.random() * 80));
        option.xAxis[0].data.shift();
        option.xAxis[0].data.push(axisData);

        if (data0[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[0] + ',请检查')
            fault_info(option.legend.data[0]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data1[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[1] + ',请检查')
            fault_info(option.legend.data[1]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data2[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[2] + ',请检查')
            fault_info(option.legend.data[2]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        if (data3[9] >= 80) {
            toastr.error('在' + new Date().Format("yyyy-MM-dd hh:mm:ss") + '时，' + option.legend.data[3] + ',请检查')
            fault_info(option.legend.data[3]+'异常', new Date().Format("yyyy-MM-dd hh:mm:ss"))

        }
        echaert.setOption(option);
    }, 4000);

    // 使用配置项和数据显示图表。
    echaert.setOption(option);
})


Date.prototype.Format = function (fmt) { //格式化时间
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


function fault_info(event,time) {//将异常发送到后台
    $.ajax({
        url: '/fault_real/',
        method: 'post',
        data: {
            'event': event,
            'time': time,
        },
        success: function (data) {

        }
    })
}


